const state = {
	ticket_list:[
		{
			id:1,
			name:'商务票',
			price:'8888',
			num:1,
			maxNum:6,
			remarks:'大会嘉宾座，参加晚宴'
		},
		{
			id:2,
			name:'VIP票',
			price:'2888',
			num:1,
			maxNum:4,
			remarks:'大会VIP座'
		},
		{
			id:3,
			name:'普通票',
			price:'1888',
			num:1,
			maxNum:16,
			remarks:'无'
		}
	]
}

const getters = {
	//获取票具列表
	getTicketList:state => state.ticket_list,
	//获取购物车列表
	addTicketList:state => {
		return state.add.map(({id,num}) => {
			//通过id来匹配，在原始数据上进行筛选
			let product = state.ticket_list.find(n => n.id == id);
			if (product) {
				return {
					...product,
					num
				}
			}
		})
	},
	totalNum:(state,getters) => {
		//getter中可以调用getter里面的方法
		let total = 0;
		getters.addTicketList.map(n => {
			total += n.num;
		})
		return total;//返回总数
	},
	//计算总价格
	totalPrice:(state,getters) => {
		let total = 0;
		getters.addTicketList.map(n => {
			total += n.num * n.price;
		})
		return total;//返回总价格
	}
}

const actions = {
	//加入购物车
	addToCart({commit},product){
		commit('addCart',{//传递一个addCart的方法，携带参数id
			id:product.id
		})
	},
	clearToCart({commit}){
		commit('clearCart')
	}
	//删除单个物品 -->

}
const mutations = {
	
}

export default{
	state,
	getters
}